<?php
/**
 * User: ppjun378
 * Data: 2019/01/
 * Time: 18:00
 */
namespace App\Service;

use App\Model\WorkExperiences;

class WorkExperiencesService
{
    /**
     * 获取数据
     * @param $request
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function dataList($resume_id)
    {
        $fields = [
            'id as word_id', // 家庭关系id
            'work_start_stop_time', //起止时间
            'company_name', //公司名称
            'department', //部门
            'position', //职位
            'compensation', //薪酬
            'leaving_reason', //离职原因
            'references_and_telephone_phone', //证明人及电话
        ];

        $data = WorkExperiences::select($fields)
            ->where('resume_id', $resume_id)
            ->orderBy('id', 'asc')
            ->get();

        $data = empty($data) ? array() : $data->toArray();

        if (!empty($data)) {
            foreach ($data as $key => $val) {
                $time = explode('/', $val['work_start_stop_time']);
                $data[$key]['work_start_time'] = $time[0];
                $data[$key]['work_stop_time'] = $time[0];
            }
        }

        return $data;

    }

    /**
     * 添加/更新
     * @param $request
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function store($request, $id)
    {
        $work_experiences = $request->input('work_experiences', '');
        // $work_experiences = '[{"work_id": 1,"work_start_stop_time": "20181001-20181231","company_name": "完胜科技公司","department": "技术部","position": "PHP工程师","compensation": "8k","leaving_reason": "公司倒闭","references_and_telephone_phone": "白俊生"}]';
        if (empty($work_experiences)) {
            return false;
        }

        $work_experiences = json_decode($work_experiences, true);

        $map = [];
        foreach ($work_experiences as $key => $val) {
            $map['work_start_stop_time'] = $val['work_start_stop_time'];
            $map['company_name'] = $val['company_name'];
            $map['department'] = $val['department'];
            $map['position'] = $val['position'];
            $map['leaving_reason'] = $val['leaving_reason'];
            $map['compensation'] = $val['compensation'];
            $map['references_and_telephone_phone'] = $val['references_and_telephone_phone'];

            if (!empty($val['work_id'])) {
                $data_exist = WorkExperiences::where('id', $val['work_id'])->first();
                if ($data_exist) {
                    // 更新信息
                    $result = WorkExperiences::where('id', $val['work_id'])->update($map);
                } else {
                    $map['resume_id'] = $id;
                    // 添加信息
                    $result = WorkExperiences::create($map);
                }

            } else {
                $map['resume_id'] = $id;
                // 添加信息
                $result = WorkExperiences::create($map);
            }
        }

    }

    /**
     * 删除
     * @param int $id
     * @return bool\Intervention
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function destory($request)
    {
        $id = $request->input('id', '');
        $res = WorkExperiences::destory($id);
        if ($res > 0) {
            return $res;
        } else {
            return false;
        }
    }

    /**
     * 恢复删除
     * @param $int $id
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function restore($request)
    {
        $id = $request->input('id', '');
        $res = WorkExperiences::restore();
        return $res;
    }

}
